<template>
  <div class="login-wrap">
    <el-form :model="ruleForm" status-icon :rules="rules" label-width="100px"  ref="ruleForm"  class="demo-ruleForm login-container">
      <h3 class="title">用户注册</h3>
      <el-form-item label="昵称" prop="name">
        <el-input type="text" v-model="ruleForm.name" auto-complete="off" placeholder="昵称"></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="account">
        <el-input type="text" v-model="ruleForm.account" auto-complete="off" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
  </div>
</template>
<script type="text/ecmascript-6">
import { login } from '../api/userMG'
import { setCookie, getCookie, delCookie } from '../utils/util'
import md5 from 'js-md5'
export default {
  name: 'login',
  data() {
    var validateUserName= (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'));
        } else {
            setTimeout(() => {
                if (this.ruleForm.account !== '') {
                    this.$refs.ruleForm.validateField('account');
                }   
            });
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
            setTimeout(() => {
                 if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
            });
         
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          password: '',
          name:'',
          account:'',
          checkPass: '',
          age: ''
        },
        rules: {
          name: [
            { validator: validateUserName, trigger: 'blur' }
          ],
          account: [
            { validator: validateUserName, trigger: 'blur' }
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ]
        }
      };
  },
  // 创建完毕状态(里面是操作)
  created() {
  },
  // 里面的函数只有调用才会执行
  methods: {
     submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log(this.ruleForm,111111111111);
            this.$axios.post('http://localhost:8888/user/saveUser', {
              name:this.ruleForm.name,
              account:this.ruleForm.account,
              password:this.ruleForm.password
            }).then(res => {
              if (res.data.id) {
                setTimeout(() => {
                  this.logining = false
                  this.$router.push({ path: '/login' })
                }, 1000)
              }
            }) 
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
  }
}
</script>

<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  background-image: url();
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}
.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}
.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.remember {
  margin: 0px 0px 35px 0px;
}
.code-box {
  text-align: right;
}
.codeimg {
  height: 40px;
}
.check{
  width: 100%;
  height: 40px;
  border-radius: 4px;
  background: #d5d6d8;
  border: 1px solid #DCDFE6;
  text-align: center;
  line-height: 40px;
  margin-bottom:22px;
}
.success-check{
  background: #e1f3d8;
}
.check:hover{
  cursor: pointer;
}
/deep/.el-button--default{
    margin-left: 40px;
}
.el-form-item__content{
    display: flex;
    justify-content: space-between;
}
</style>
